// This file contains material supporting section 3.7 of the textbook:
// "Object Oriented Software Engineering" and is issued under the open-source
// license found at www.lloseng.com 

//package clientServer;

import java.io.IOException;

import javax.swing.JOptionPane;

import ocsf.client.AbstractClient;

import common.ChatIF;
import common.MyFile;
/**
 * This class overrides some of the methods defined in the abstract
 * superclass in order to give more functionality to the client.

 */

public class ChatClient extends AbstractClient
{
/**
 * The interface type variable.  It allows the implementation of 
 * the display method in the client.
 */
  ChatIF clientUI; 
  /**
   * holds the result, generated in the server and sent back to client (String result)
   */
  public static String result;
	/**
   * holds the result2, only for the CountReviews thread, generated in the server and sent back to client (String result)
   */
  public static String result2;
  /**
   * holds the Object containing the file sent from the server
   */
  public static MyFile resFile;
  /**
   * determine if the user is frozen or not
   */
  int frozen=0;
  
  
  /*****************************************************ChatClient************************************/
  /**
   * Constructs an instance of the chat client.
   *
   * @param host The server to connect to.
   * @param port The port number to connect on.
   * @param clientUI The interface type variable.
   */
  public ChatClient(String host, int port, ClientQuery clientUI) 
    throws IOException 
  {
    super(host, port); //Call the superclass constructor
    this.clientUI = clientUI;
  
    openConnection();
  }
  
  /***************************************************handleMessageFromServer**********************/
  /**
   * This method handles all data that comes in from the server.
   * Saves the result Received from server in result or result2 (According to the type of result : in res[1])
   * @param msg The message from the server.
   */
  public void handleMessageFromServer(Object msg) 
  {
	  
	  if(msg instanceof String[])
	  {
			  
		  String[] res=(String[])msg;
		  if((res[1].equals("getReviewCount")))
		  {			
			  result2=res[2];
		      Main.log.flag2=true;					
		  }
		  else if((res[1].equals("frozen")))
		  {
			  frozen=1;
			 this.quit();
		}
		  else if(!(res[1].equals("logout")))
		  {	
			result=res[2];
			if(res[1].equals("FAIL"))
				this.quit(); 
			else 
			Main.log.flag=true;					
		 }
		 else
			this.quit();
	       
	  }
	  if(msg instanceof MyFile)
	  {
		  MyFile file=(MyFile)msg;
		  setFileObj(file);
		  Main.log.flag=true;
		  
	  }

	  
	 
	  
  }//handleMessageFromServer
  
  /**
   * set a string result, to result;
   * @param msg
   */


  public static void setRs(String msg)
  {

	  result=msg; 	 
  }
  /**
   *  getter for result 2
   * @return String
   */
  public static String getRs2()
  {
    return result2;
  }
  /**
   * set an object containing a file, to resFile
   */
  public static void setFileObj(MyFile msg)
  {
	  resFile=msg;
  }
  
 /**
  * getter of result
  * @return String
  */
 
  public static String getRs()
  {
    return result;
  }
  /**
   * getter of resFile
   * @return String
   */
  public static MyFile getFileObj()
  {
	  return resFile;
  }
  
    
  /*******************************************************handleMessageFromClientUI***********************************************/
  /**
   * This method handles all data coming from the UI            
   *
   * @param message The message from the UI.    
   */
  @SuppressWarnings("deprecation")
public void handleMessageFromClientUI(String message)  
  {
    try
    {
       
    	if(message.equals("login"))	 
        	sendToServer(Main.log.loginData);
    	else if(message.equals("cart"))
        	sendToServer(Main.cart.query);
    	else if(message.equals("gReview"))
        	sendToServer(Main.addReview.query);
    	else if(message.equals("purchase"))
        	sendToServer(Main.myBooksControl.purchased);
      	else if(message.equals("YgetFieldsSubject"))
        	sendToServer(Main.welcomeScreen.searchData3);
    	else if(message.equals("getSubjects2")|| message.equals("YgetFields"))
        	sendToServer(Main.searchBookC.searchData2);
    	else if(message.equals("search"))
        	sendToServer(Main.searchBookC.searchData);
    	else if(message.equals("getdetails") || message.equals("getlanguage"))
               	sendToServer(Main.welcomeScreen.details);
    	else if(message.equals("update"))
        	sendToServer(Main.upDateBookC.upDateData);
    	else if(message.equals("addbook"))
        	sendToServer(Main.addNBookC.addBook);
    	else if(message.equals("removefc"))
        	sendToServer(Main.removeFromCC.remove_bookFC);
    	else if(message.equals("check_in_dataB"))
        	sendToServer(Main.addNBookC.check);
    	else if(message.equals("searchR"))
        	sendToServer(Main.searchReviewC.searchRe);
    	else if(message.equals("getreview"))
        	sendToServer(Main.searchReviewC.review);
    	else if(message.equals("retriveDB"))
        	sendToServer(Main.restoreBC.dbData);
    	else if(message.equals("restoreB"))
        	sendToServer(Main.restoreBC.dbRData);
    	else if(message.equals("searchuseru"))
        	sendToServer(Main.pdmc.searchu);
    	else if(message.equals("searchuserf"))
        	sendToServer(Main.pdmc.searchf);
    	else if(message.equals("updateu"))
        	sendToServer(Main.pdmc.updateu);
    	else if(message.equals("updatef"))
        	sendToServer(Main.pdmc.updatef);
    	else if(message.equals("searchuserreport")) 
        	sendToServer(Main.rrc.search); 
    	else if(message.equals("searchbookp"))
        	sendToServer(Main.brc.searchp);
    	else if(message.equals("searchbooks"))
        	sendToServer(Main.brc.searchs);
    	else if(message.equals("searchupdate"))
        	sendToServer(Main.brc.update);
    	else  if(message.equals("getFields") || message.equals("getFieldsSubject"))
        	sendToServer(Main.bookManagementCTRL.AddSubjectCTRL.searchData);
    	else if(message.equals("getFields2"))
           	sendToServer(Main.bookManagementCTRL.AddFieldCTRL.searchData); 
    	else if(message.equals("addField"))
           	sendToServer(Main.bookManagementCTRL.AddFieldCTRL.FieldData);
    	else if(message.equals("RemoveField"))
           	sendToServer(Main.bookManagementCTRL.AddFieldCTRL.FieldData);
    	else if(message.equals("getSubjects"))
        	sendToServer(Main.bookManagementCTRL.AddSubjectCTRL.searchData2);       
    	else if(message.equals("addSubject")|| message.equals("RemoveSubject"))
        	sendToServer(Main.bookManagementCTRL.AddSubjectCTRL.subjectData);
    	else if(message.equals("getUnpostedReviews"))
        	sendToServer(Main.revManagementCTRL.ReviewData);
    	else if(message.equals("ReadUnpostedReview"))
        	sendToServer(Main.revManagementCTRL.Reviewinfo);
    	else if(message.equals("ConfirmReview") || message.equals("RemoveReview")) 
        	sendToServer(Main.revManagementCTRL.insertReview);
    	else if(message.equals("getReviewCount"))
        	sendToServer(Main.welcomeScreen.ReviewCount);
       	else if(message.equals("getSubjects1")|| message.equals("getFieldsSubject1")|| message.equals("GetBooksSubjects")) 
        	sendToServer(Main.welcomeScreen.SubjectsData);  
    	else if(message.equals("AddSubjectForBook")|| message.equals("RemoveSubjectForBook"))  
    		sendToServer(Main.welcomeScreen.SubjectsData);        
    	else if(message.equals("logout"))
    	{
    		if(Main.gusers!=null)
    		{
    			if(Main.gusers.authorozations==2 || Main.gusers.authorozations==3)
    				Main.welcomeScreen.CountReviewsThread.stop();
    			sendToServer(Main.log.loginData);
    		}
    		else
        	   		sendToServer(Main.log.loginData);
        	
    	}
    	
			
    }
    catch(IOException e)
    {
      clientUI.display
        ("Could not send message to server.  Terminating client.");
      JOptionPane.showMessageDialog(null,"Could not send message to server.  Terminating client.","CCRM Error",JOptionPane.ERROR_MESSAGE);
      System.exit(1);
    }
  }
  
  /*********************************************quit**********************************************/
  /**
   * This method terminates the client.
   */
   public void quit()
  {
	   Main.log.flag=true;
   	if(frozen==1)
   	{
   		Main.log.loginData=new String[3];
   		Main.log.loginData[0]=Main.myIP;
		Main.log.loginData[1]="logout";
		Main.log.loginData[2]=Main.gusers.id;
		Main.log.connection= new ClientQuery(Main.serverIP,5555);
		Main.log.connection.accept("logout");
   		 JOptionPane.showMessageDialog(null,"Your account has been frozen.","ERROR",JOptionPane.ERROR_MESSAGE);
   		System.exit(1);
   	}
    try
    {
    	    		closeConnection();
    }
    catch(IOException e) {}
   
  }

}
//End of ChatClient class
 